﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstituteManager.Bussiness;
using InstituteManager.Data;
using System.Security.Cryptography;

namespace InstituteManager.adm
{
    public partial class Award : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pnlMessage.Visible = false;
            if (!IsPostBack)
            {
                lbtDeleteT.Attributes.Add("onClick", "javascript:return confirm(\"Are you sure ?\")");
                lbtDeleteB.Attributes.Add("onClick", "javascript:return confirm(\"Are you sure ?\")");
                gridBind();
            }
        }
        protected void gridBind()
        {
            grvView.DataSource = AwardBL.getByAll();
            grvView.DataBind();
        }

   

        protected void ResetControl()
        {
            txtAwardName.Text = "";
            txtWorth.Text = "";
            txtDescription.Text = "";

        }

        protected void lbtAddnew_Click(object sender, EventArgs e)
        {
            pnlView.Visible = false;
            pnlUpdate.Visible = true;
            ResetControl();
        }

        protected void lbtDelete_Click(object sender, EventArgs e)
        {
            bool result = true;
            bool isChecked = false;
            foreach (GridViewRow row in grvView.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    if (((CheckBox)row.FindControl("chkSelect")).Checked == true)
                    {
                        isChecked = true;
                        if (!AwardBL.Delete(int.Parse(grvView.DataKeys[row.RowIndex].Value.ToString())))
                            result = false;
                    }
                }
            }

            if (isChecked == false)
                ShowMessage(-1, "You have not selected data to delete ! ");
            else if (result)
            {
                ShowMessage(1, "Data deleted successfully ! ");
                gridBind();
            }
            else
            {
                ShowMessage(0, "Data deleted fail : " + AwardBL.errorMsg);
            }
        }

        protected void lbtRefresh_Click(object sender, EventArgs e)
        {
            gridBind();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            string strId = txtId.Text;

            Data.tbl_Award entt = new Data.tbl_Award();
            entt.AwardName = txtAwardName.Text;
            entt.Worth = int.Parse(txtWorth.Text);
            entt.Description = txtDescription.Text;            

            // if strId = "" then add new else update
            if (strId.Trim() == "")
            {
                if (AwardBL.AddNew(entt))
                {
                    ShowMessage(1, "Data added successfully ! ");
                    gridBind();
                }
                else
                {
                    ShowMessage(0, "Data added fail : " + AwardBL.errorMsg);
                }
            }
            else
            {
                entt.AwardId = int.Parse(strId);
                if (AwardBL.Update(entt))
                {
                    ShowMessage(1, "Data updated successfully ! ");
                    gridBind();
                }
                else
                {
                    ShowMessage(0, "Data updated fail : " + AwardBL.errorMsg);
                }
            }

            pnlView.Visible = true;
            pnlUpdate.Visible = false;
            gridBind();
        }

        protected void ShowMessage(int MegType, string Message)
        {
            pnlMessage.Visible = true;
            ((Label)pnlMessage.FindControl("lblMessage")).Text = Message;
            switch (MegType)
            {
                case 1:
                    pnlMessage.CssClass = "sukses";
                    break;
                case 0:
                    pnlMessage.CssClass = "gagal";
                    break;
                default:
                    pnlMessage.CssClass = "informasi";
                    break;
            }
        }

        protected void lbtBack_Click(object sender, EventArgs e)
        {
            pnlView.Visible = true;
            pnlUpdate.Visible = false;
        }

        protected void grvView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvView.PageIndex = e.NewPageIndex;
            gridBind();
        }

        protected void grvView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strId = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "cmdEdit":
                    txtId.Text = strId;
                    tbl_Award obj = AwardBL.getById(int.Parse(strId));
                    txtAwardName.Text = obj.AwardName;
                    txtWorth.Text = obj.Worth.ToString();
                    txtDescription.Text = obj.Description;
                    //ddlCompetitionId.SelectedValue = obj.tbl_Competition.CompetitionId.ToString();

                    pnlUpdate.Visible = true;
                    pnlView.Visible = false;
                    break;
                case "cmdView":
                    obj = AwardBL.getById(int.Parse(strId));
                    lblAwardName.Text = obj.AwardName;
                    lblWorth.Text = obj.Worth.ToString();
                    lblDescription.Text = obj.Description;
                    //lblCompetitionId.Text = obj.tbl_Competition.CompetitionId.ToString();

                    pnlView.Visible = false;
                    pnlDetail.Visible = true;
                    break;
                case "cmdDelete":
                    if (AwardBL.Delete(int.Parse(strId)))
                    {
                        ShowMessage(1, "Data deleted successfully ! ");
                        gridBind();
                    }
                    else
                    {
                        ShowMessage(0, "Data deleted fail : " + AwardBL.errorMsg);
                    }
                    break;
            }
        }

        protected void grvView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
                ((CheckBox)e.Row.FindControl("chkSelectAll")).Attributes.Add("onClick", "SelectAllClick(this)");
            if (e.Row.RowType == DataControlRowType.DataRow)
                ((CheckBox)e.Row.FindControl("chkSelect")).Attributes.Add("onClick", "SelectClick(this)");
        }
        protected void ibtSearch_Click(object sender, ImageClickEventArgs e)
        {
            grvView.DataSource = AwardBL.SearchByName(txtSearch.Text);
            DataBind();
        }

        protected void lbtBackDetail_Click(object sender, EventArgs e)
        {
            pnlDetail.Visible = false;
            pnlView.Visible = true;
        }
    }
}